var factoryOSArr = new Array();
var factoryOSIDArr = new Array();
var factoryNameArr = new Array();
var factoryIDArr = new Array();
var factoryOSOrderArr = new Array();
var factoryNameOrderArr = new Array();
var factoryOSSel;
var factoryNameSel;
var factoryPeroidSel;

var phoneTypes = new Array();
var phoneTypesWPid  = new Array();
var osTypes = new Array();
var factoryTypes = new Array();
var factoryDates = new Array();
var factoryData = new Array();

//加载厂商名和厂商平台下拉列表
function loadFactoryList(data) {
	factoryOSArr = new Array();
	factoryOSIDArr = new Array();
	factoryNameArr = new Array();
	factoryIDArr = new Array();
	factoryOSOrderArr = new Array();
	factoryNameOrderArr = new Array();
	factoryOSSel = document.getElementById("factoryOS");
	factoryNameSel  = document.getElementById("factoryName");
	factoryPeroidSel  = document.getElementById("factoryPeroidSel");
	factoryOSSel.options.length = 0;
	factoryNameSel.options.length = 0;
	factoryOSSel.options.add(new  Option("全部平台", ""));
	factoryNameSel.options.add(new  Option("全部厂商", ""));

	$(data.items).each(function(i, value){
		var fOS = value["os"];
		var fOSID = value["osid"];
		var fName = value["fname"];
		var fID = value["fid"];
		fosi = in_array(fOS, factoryOSArr);
		if(fosi == -1) {
			factoryOSArr.push(fOS);
			factoryOSIDArr.push(fOSID);
			fosi=factoryOSArr.length-1;
			factoryOSOrderArr[fosi] = new Array();
			factoryOSOrderArr[fosi][0] = new Array(); //厂商ID
			factoryOSOrderArr[fosi][1] = new Array(); //厂商名
			factoryOSSel.options.add(new  Option(fOS, fOSID));
		}
		factoryOSOrderArr[fosi][0].push(fID);
		factoryOSOrderArr[fosi][1].push(fName);

		fnamei = in_array(fName, factoryNameArr);
		if(fnamei == -1) {
			factoryNameArr.push(fName);
			factoryIDArr.push(fID);
			fnamei=factoryNameArr.length-1;
			factoryNameOrderArr[fnamei] = new Array();
			factoryNameOrderArr[fnamei][0] = new Array(); //OSID
			factoryNameOrderArr[fnamei][1] = new Array(); //OS名
			factoryNameSel.options.add(new  Option(fName, fID));
		}
		factoryNameOrderArr[fnamei][0].push(fOSID);
		factoryNameOrderArr[fnamei][1].push(fOS);
//		alert(fName+ " " + fOS);
	});
	jQuery('#body-wrapper').hideLoading();
}

//改变场上平台后更新厂商名下拉列表的值
function changeFactoryOS(obj) {
	//如果厂商和平台都被选择，显示起始日期选择区域，否则显示时间区域选择项
	if (factoryOSSel.selectedIndex > 0  && factoryNameSel.selectedIndex >0)
	{
		$("#factoryDate").show();
		$("#factoryPeriod").hide();
	}
	else {
		$("#factoryDate").hide();
		$("#factoryPeriod").show();
	}

	selectedFName = factoryNameSel.options[factoryNameSel.selectedIndex].text;
	fosi = in_array(obj.options[obj.selectedIndex].text, factoryOSArr);
	factoryNameSel.options.length=0;
	factoryNameSel.options.add(new  Option("全部厂商", ""));
	if(fosi != -1) {
		for(i=0; i<factoryOSOrderArr[fosi][0].length; i++) {
			factoryNameSel.options.add(new  Option(factoryOSOrderArr[fosi][1][i], factoryOSOrderArr[fosi][0][i]));
			if (selectedFName == factoryOSOrderArr[fosi][1][i])
				factoryNameSel.selectedIndex = i+1;
		}
	}
	else {
		for(i=0; i<factoryNameArr.length; i++) {
			factoryNameSel.options.add(new  Option(factoryNameArr[i], factoryIDArr[i]));
			if (selectedFName == factoryNameArr[i])
				factoryNameSel.selectedIndex = i+1;
		}
	}
}

//改变厂商名选项后更新平台下拉列表的值
function changeFactoryName(obj) {
	//如果厂商和平台都被选择，显示起始日期选择区域，否则显示时间区域选择项
	if (factoryOSSel.selectedIndex > 0  && factoryNameSel.selectedIndex >0)
	{
		$("#factoryDate").show();
		$("#factoryPeriod").hide();
	}
	else {
		$("#factoryDate").hide();
		$("#factoryPeriod").show();
	}

	selectedFOS = factoryOSSel.options[factoryOSSel.selectedIndex].text;
	fnamei = in_array(obj.options[obj.selectedIndex].text, factoryNameArr);
	factoryOSSel.options.length=0;
	factoryOSSel.options.add(new  Option("全部平台", ""));
	if(fnamei != -1) {
		for(i=0; i<factoryNameOrderArr[fnamei][0].length; i++) {
			factoryOSSel.options.add(new  Option(factoryNameOrderArr[fnamei][1][i], factoryNameOrderArr[fnamei][0][i]));
			if (selectedFOS == factoryNameOrderArr[fnamei][1][i])
				factoryOSSel.selectedIndex = i+1;
		}
	}
	else {
		for(i=0; i<factoryOSArr.length; i++) {
			factoryOSSel.options.add(new  Option(factoryOSArr[i], factoryOSIDArr[i]));
			if (selectedFOS == factoryOSArr[i])
				factoryOSSel.selectedIndex = i+1;
		}
	}
}

function getFactoryGeneralData() {
	var url = "";
	var fid = factoryNameSel.options[factoryNameSel.selectedIndex].value;
	var osid = factoryOSSel.options[factoryOSSel.selectedIndex].value;
	var startDate = new Date();
	var endDate = new Date();

	if (factoryOSSel.selectedIndex > 0  || factoryNameSel.selectedIndex >0)
	{
		jQuery('#body-wrapper').showLoading();
		if (factoryOSSel.selectedIndex > 0  && factoryNameSel.selectedIndex >0)
		{
			startDate.setFullYear(($("#FactoryGeneralContentStartDate").val()).substring(0,4), ($("#FactoryGeneralContentStartDate").val()).substring(5,7)-1,($("#FactoryGeneralContentStartDate").val()).substring(8,10));
			endDate.setFullYear(($("#FactoryGeneralContentEndDate").val()).substring(0,4), ($("#FactoryGeneralContentEndDate").val()).substring(5,7)-1,($("#FactoryGeneralContentEndDate").val()).substring(8,10));

			days = parseInt((endDate - startDate)/1000/60/60/24)+1;

			if (days > 0)
			{
				url = "http://10.103.13.15:8000/client/factoryAnalytics?fid="+fid+"&osid="+osid+"&staDate="+$("#FactoryGeneralContentStartDate").val()+"&pd="+days;
			}
			else 
				alert("选择区间不对");
		}
		else if (factoryOSSel.selectedIndex > 0)
		{
			url = "http://10.103.13.15:8000/client/factoryComp?osid="+osid+"&period="+factoryPeroidSel.options[factoryPeroidSel.selectedIndex].value

		}
		else if (factoryNameSel.selectedIndex >0)
		{
			url = "http://10.103.13.15:8000/client/factoryOSComp?fid="+fid+"&period="+factoryPeroidSel.options[factoryPeroidSel.selectedIndex].value
		}
		$.ajax({
			type: "GET",
			url: url,
			cache: true,
			dataType : "jsonp",
			jsonnp: "callback",
			success: loadFactoryData
		});
	}
	else {
		alert("平台或厂商至少选择一项！");
	}
}


function loadFactoryData(data) {
	phoneTypes = new Array(); 
	phoneTypesWPid = new Array();
	osTypes = new Array();
	factoryTypes = new Array();

	factoryDates = new Array();
	factoryData = new Array();
	var uv, nu, tu, pid, pname, ph, os, factory;
	if (data.items.length > 0) {

	var startDate = new Date();
	var endDate = new Date();
	var dateStr = "";

	if(factoryPeroidSel.options[factoryPeroidSel.selectedIndex].value == "w") 
			startDate.setDate(startDate.getDate()-7);
	else if(factoryPeroidSel.options[factoryPeroidSel.selectedIndex].value == "m") 
			startDate.setDate(startDate.getDate()-30);
	else if(factoryPeroidSel.options[factoryPeroidSel.selectedIndex].value == "q") 
			startDate.setDate(startDate.getDate()-90);
	else if(factoryPeroidSel.options[factoryPeroidSel.selectedIndex].value == "y") 
			startDate.setDate(startDate.getDate()-365);
	endDate.setDate(endDate.getDate()-1);
	dateStr = startDate.getFullYear()+ "-" + (startDate.getMonth()+1)+ "-" + startDate.getDate() + " 至 " + endDate.getFullYear()+ "-" + (endDate.getMonth()+1)+ "-" + endDate.getDate();


		if (factoryOSSel.selectedIndex > 0  && factoryNameSel.selectedIndex >0)
		{
			$(data.items).each(function(i, value){

				var myDate=new Date()
				year = value["year"];
				month = value["month"];
				day = value["day"];
				myDate.setFullYear(year,month-1,day);
				factoryDates.push(year + "/" + month + "/" + day);
				var factoryDetails = value["data"];
				var phoneType = "";
				var phoneTypeWPid = "";
				$(factoryDetails).each(function(l, dvalue){
					pid = dvalue["pid"];
					pname = dvalue["pname"];
					ph = dvalue["ph"];
					uv = dvalue["uv"]; //每日使用设备数
					nu = dvalue["nu"]; //新增设备数

					if (typeof(dvalue["uv"])=="undefined")
						uv = 0;
					if (typeof(dvalue["nu"])=="undefined")
						nu = 0;

					if (ph == "") {
						phoneType = pname;
						phoneTypeWPid = pname + " " + pid;
					}
					else {
						phoneType = ph;
						phoneTypeWPid = ph + " " + pid;
					}
					pi = in_array(phoneTypeWPid, phoneTypesWPid);
					if(pi == -1) {
						phoneTypes.push(phoneType);
						phoneTypesWPid.push(phoneTypeWPid);
						pi=phoneTypes.length-1;
						factoryData[pi] = new Array();
						factoryData[pi][0] = new Array();
						factoryData[pi][1] = new Array();	
					}

					if(factoryDates.length-factoryData[pi][0].length > 1) {
						var size = factoryDates.length-factoryData[pi][0].length;
						for(l=1; l<size; l++) {
							factoryData[pi][0].push(0);
							factoryData[pi][1].push(0);	
						}
					}

					factoryData[pi][0].push(uv);
					factoryData[pi][1].push(nu);
				});

			});
			$(".bitem").removeClass("selbitem");
			$("#FactoryGeneralContentTab0").addClass("selbitem");	
			genTable("FactoryGeneralContentTable", phoneTypes, factoryDates, factoryData, 0);
			genLineChart("FactoryGeneralContentLine","", factoryDates, phoneTypes, factoryData, 0);
			$("#FactoryGeneralResult1").show();
			$("#FactoryGeneralResult2").hide();
		}
		else if (factoryOSSel.selectedIndex > 0)
		{

			$(data.items).each(function(i, value){
				factory = value["factory"];
				tnu = value["tnu"];
				if (typeof(tnu)=="undefined")
					tnu = 0;

				factoryTypes.push(factory);
				pi= factoryData.length;
				factoryData[pi] = new Array();
				factoryData[pi][0] = new Array();
				factoryData[pi][0].push(tnu);
			});
			genPieChart("FactoryGeneralContentPie", factoryOSSel.options[factoryOSSel.selectedIndex].text + "平台" + factoryNameSel.options[factoryNameSel.selectedIndex].text + "累计新增设备数百分比图", factoryTypes, factoryData, 0, dateStr);
			$(".bitem").removeClass("selbitem");
			$("#FactoryGeneralContentTab3").addClass("selbitem");	
			$("#FactoryGeneralResult2").show();
			$("#FactoryGeneralResult1").hide();
		}
		else if (factoryNameSel.selectedIndex >0)
		{

			$(data.items).each(function(i, value){
				os = value["os"];
				tnu = value["tnu"];
				if (typeof(tnu)=="undefined")
					tnu = 0;

				osTypes.push(os);
				pi= factoryData.length;
				factoryData[pi] = new Array();
				factoryData[pi][0] = new Array();
				factoryData[pi][0].push(tnu);
			});

			genPieChart("FactoryGeneralContentPie", factoryNameSel.options[factoryNameSel.selectedIndex].text + factoryOSSel.options[factoryOSSel.selectedIndex].text + "累计新增设备数百分比图", osTypes, factoryData, 0, dateStr);
			$(".bitem").removeClass("selbitem");
			$("#FactoryGeneralContentTab3").addClass("selbitem");	
			$("#FactoryGeneralResult2").show();
			$("#FactoryGeneralResult1").hide();
		}
		$(".tab-content").show();	

	}
	else {
		alert("选择的时间区间内无数据");	
		$(".tab-content").hide();	

	}


	jQuery('#body-wrapper').hideLoading();
}


//tab切换
function changeFactoryGeneralContentValueType(typeValue) {


	genTable("FactoryGeneralContentTable", phoneTypes, factoryDates, factoryData, typeValue);
	genLineChart("FactoryGeneralContentLine","", factoryDates, phoneTypes, factoryData, typeValue);


	$(".bitem").removeClass("selbitem");
	$("#FactoryGeneralContentTab"+typeValue).addClass("selbitem");			
	
}